package com.conn;

import java.io.*;
import java.util.*;

public class ClientMain {
    Client cli;

    public static void main(String[] args) {
        try {
            new ClientMain().start();
        } catch (IOException ex) {
            ex.printStackTrace();
            System.out.println("Client init failed!");
            return ;
        }
    }

    public ClientMain() throws IOException {
        cli = new Client(); 
        System.out.println("hi! welcome use simple talk");
    }

    private String getItem(String s) {
        Scanner sc = new Scanner(System.in);
        System.out.print("your "+s+": ");
        return sc.nextLine();
    }

    public boolean register() {
        RegisterInfo ri = new RegisterInfo();
        ri.username = getItem("username");
        ri.passwd = getItem("passwd");
        ri.email = getItem("email");
        ri.name = getItem("name");
        ri.sex = getItem("sex");
        ri.birthday = getItem("birthday");
        ri.phone = getItem("phone");
        ri.country = getItem("country");
        ri.home = getItem("home");
        return cli.clientRegister(ri);
    }

    public boolean login() {
        ClientInfo ci = new ClientInfo();
        ci.username = getItem("username");
        ci.passwd = getItem("passwd");
        return cli.clientLogin(ci);
    }

    public void start() {
        Scanner sc = new Scanner(System.in);
        String option;
        byte[] byteBuf = new byte[1024];
        
        while (true) {
            System.out.println("1 Login\n2 Register\n3 Secret help");
            System.out.print("Your choice: ");
            option = sc.nextLine();
            if (option.equals("2")) {
                System.out.println("start register.");
                register();
            } else if (option.equals("1")) {
                System.out.println("start login.");
                login();
            } else if (option.equals("3")) {
                System.out.println("start secret help.");
            } else  {
                System.out.print("Warning! Unknow option!\n");
            }
        }
    } 
}

//        //test
//        try {
//            cli.clientUnregister(null);
//        } catch (UnsupportedOperationException ex) {
//            System.out.println("UnsupportedOperationException call!");
//        }

